#!/usr/bin/env pwsh
# MAESS智能体系统一键部署脚本（Windows版）
# 运行方式：右键以PowerShell管理员身份运行，或在PowerShell中执行：.\deploy_maess.ps1

# 配色方案
$ErrorColor = "Red"
$SuccessColor = "Green"
$InfoColor = "Cyan"
$WarningColor = "Yellow"

# 检查PowerShell版本
Write-Host "正在检查PowerShell版本..." -ForegroundColor $InfoColor
if ($PSVersionTable.PSVersion.Major -lt 5) {
    Write-Host "错误：需要PowerShell 5.0或更高版本！" -ForegroundColor $ErrorColor
    exit 1
}

# 启用脚本执行策略
Write-Host "正在配置PowerShell执行策略..." -ForegroundColor $InfoColor
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force

# 设置项目根目录
$ProjectRoot = $PSScriptRoot
Write-Host "项目根目录: $ProjectRoot" -ForegroundColor $InfoColor

# 检查Python是否安装
Write-Host "正在检查Python环境..." -ForegroundColor $InfoColor
$PythonPath = Get-Command python -ErrorAction SilentlyContinue
if (-not $PythonPath) {
    Write-Host "错误：未找到Python，请先安装Python 3.8+！" -ForegroundColor $ErrorColor
    Write-Host "推荐下载地址：https://www.python.org/downloads/windows/" -ForegroundColor $WarningColor
    exit 1
}

# 检查Python版本
$PythonVersion = python --version 2>&1
Write-Host "当前Python版本: $PythonVersion" -ForegroundColor $InfoColor

# 创建虚拟环境
$VenvDir = Join-Path -Path $ProjectRoot -ChildPath ".venv"
if (-not (Test-Path -Path $VenvDir)) {
    Write-Host "正在创建虚拟环境..." -ForegroundColor $InfoColor
    python -m venv $VenvDir
    if ($LASTEXITCODE -ne 0) {
        Write-Host "错误：创建虚拟环境失败！" -ForegroundColor $ErrorColor
        exit 1
    }
    Write-Host "虚拟环境创建成功！" -ForegroundColor $SuccessColor
}

# 激活虚拟环境
$ActivateScript = Join-Path -Path $VenvDir -ChildPath "Scripts\Activate.ps1"
Write-Host "正在激活虚拟环境..." -ForegroundColor $InfoColor
try {
    & $ActivateScript
    Write-Host "虚拟环境激活成功！" -ForegroundColor $SuccessColor
} catch {
    Write-Host "错误：激活虚拟环境失败！" -ForegroundColor $ErrorColor
    exit 1
}

# 升级pip
Write-Host "正在升级pip..." -ForegroundColor $InfoColor
pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple/
if ($LASTEXITCODE -ne 0) {
    Write-Host "警告：pip升级失败，继续执行..." -ForegroundColor $WarningColor
}

# 安装依赖（使用国内源）
$RequirementsFile = Join-Path -Path $ProjectRoot -ChildPath "requirements_cn.txt"
if (Test-Path -Path $RequirementsFile) {
    Write-Host "正在安装项目依赖（使用国内源）..." -ForegroundColor $InfoColor
    pip install -r $RequirementsFile -i https://mirrors.aliyun.com/pypi/simple/
    if ($LASTEXITCODE -ne 0) {
        Write-Host "警告：部分依赖安装失败，可能需要手动处理！" -ForegroundColor $WarningColor
    } else {
        Write-Host "依赖安装完成！" -ForegroundColor $SuccessColor
    }
}

# 检查环境变量文件
$EnvFile = Join-Path -Path $ProjectRoot -ChildPath ".env"
$EnvExampleFile = Join-Path -Path $ProjectRoot -ChildPath ".env.example"
if (-not (Test-Path -Path $EnvFile) -and (Test-Path -Path $EnvExampleFile)) {
    Write-Host "正在创建环境变量文件..." -ForegroundColor $InfoColor
    Copy-Item -Path $EnvExampleFile -Destination $EnvFile
    Write-Host "请编辑 .env 文件配置环境变量！" -ForegroundColor $WarningColor
}

# 初始化数据库
Write-Host "正在初始化数据库..." -ForegroundColor $InfoColor
$InitScript = Join-Path -Path $ProjectRoot -ChildPath "init_mysql.py"
if (Test-Path -Path $InitScript) {
    python $InitScript
    if ($LASTEXITCODE -ne 0) {
        Write-Host "警告：数据库初始化可能失败，请检查数据库配置！" -ForegroundColor $WarningColor
    } else {
        Write-Host "数据库初始化完成！" -ForegroundColor $SuccessColor
    }
}

# 检查Redis连接
Write-Host "正在检查Redis连接..." -ForegroundColor $InfoColor
$CheckRedisScript = Join-Path -Path $ProjectRoot -ChildPath "check_redis.py"
if (Test-Path -Path $CheckRedisScript) {
    python $CheckRedisScript
    if ($LASTEXITCODE -ne 0) {
        Write-Host "警告：Redis连接失败，请确保Redis服务已启动！" -ForegroundColor $WarningColor
    } else {
        Write-Host "Redis连接成功！" -ForegroundColor $SuccessColor
    }
}

# 启动Celery worker（后台运行）
Write-Host "正在启动Celery worker..." -ForegroundColor $InfoColor
Start-Process -FilePath "powershell.exe" -ArgumentList "-NoExit -Command \`